package com.lanchetech.service.impl;

import com.lanchetech.bean.response.ProductResp;
import com.lanchetech.bean.response.PublicResp;
import com.lanchetech.bean.vo.ShareActivitySpuVo;
import com.lanchetech.bean.vo.ShareSkuVo;
import com.lanchetech.dao.ShareActivityMapper;
import com.lanchetech.dao.SkuMapper;
import com.lanchetech.entity.ShareActivity;
import com.lanchetech.service.PublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
public class PublicServiceImpl implements PublicService {

    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private ShareActivityMapper shareActivityMapper;

    @Override
    public PublicResp setShareInformation(List<ShareActivitySpuVo> shareActivitySpuVos) {
        PublicResp publicResp = new PublicResp();
        BigDecimal maxCommission = BigDecimal.ZERO;
        BigDecimal maxHiCoin = BigDecimal.ZERO;
        for (ShareActivitySpuVo shareActivitySpuVo : shareActivitySpuVos) {
            if (shareActivitySpuVo.getType()) {
                // 固定金额  最高可得佣金
                BigDecimal regularCommission = shareActivitySpuVo.getCommissionScale();
                // 固定金额  最高可得嗨币
                BigDecimal regularHiCoin = shareActivitySpuVo.getHiCoinScale();
                if (regularCommission.compareTo(maxCommission) > 0) maxCommission = regularCommission;
                if (regularHiCoin.compareTo(maxHiCoin) > 0) maxHiCoin = regularHiCoin;
            } else {
                ShareSkuVo shareSkuVo = skuMapper.getSkuById(shareActivitySpuVo.getSkuId());
                // 百分比    最高可得佣金
                BigDecimal percentageCommission = shareActivitySpuVo.getCommissionScale().multiply(shareSkuVo.getPrice()).setScale(2, RoundingMode.HALF_DOWN);
                // 百分比    最高可得嗨币
                BigDecimal percentageHiCoin = shareActivitySpuVo.getHiCoinScale().multiply(shareSkuVo.getPrice()).setScale(2, RoundingMode.HALF_DOWN);
                if (percentageCommission.compareTo(maxCommission) > 0) maxCommission = percentageCommission;
                if (percentageHiCoin.compareTo(maxHiCoin) > 0) maxHiCoin = percentageHiCoin;
            }
        }
        publicResp.setShare(true);
        publicResp.setShareActivityId(shareActivitySpuVos.get(0).getShareActivityId());
        publicResp.setShareMaxCommPrice(maxCommission);
        publicResp.setShareHiCoinMaxPrice(maxHiCoin);
        return publicResp;
    }
}

